<?php
$menumark = 'zhufang';
switch ($act) {

    case 'write':
        //订单编号
        if (isGet()) {
            $order_number = getMillisecond() . rand(1, 100);
            $house = $db->pe_select('house', array('house_id' => $_g_houseid));
            $total_price = $house['house_storeprice'];
            $day = 1;

            if (!empty($_g_in_date) && !empty($_g_out_date)) {
                //入住时间
                $day = diffBetweenTwoDays($_g_in_date, $_g_out_date);
                $total_price = $house['house_storeprice'] * $day;

            } else {

            }


        } else if (isPost()) {
            $_p_info['order_number'] = getMillisecond() . rand(1, 100);
            $_p_info['order_houseid'] = $_g_houseid;
            $_p_info['order_state'] = 1;
            $_p_info['order_housenumber'] = $_g_h_number;
            $_p_info['order_payway'] = 1;
            $_p_info['order_subdate'] = date('Y-' .
                'm-d H-i-s', time());
            $_p_info['order_ostate'] = 2;
            if ($_g_in_date != "") {
                $in_date = strtotime($_g_in_date . ' 12:00:00');

                $_p_info['order_startdate'] = $in_date;
            } else {
                $in_date = strtotime(date('Y-m-d', time()) . ' 12:00:00');

                $_p_info['order_startdate'] = $in_date;
            }
            if ($_g_out_date != "") {
                $out_date = strtotime($_g_out_date . ' 12:00:00');

                $_p_info['order_enddate'] = $out_date;
            } else {
                $out_date = strtotime(date("Y-m-d", strtotime("+1 day")) . ' 12:00:00');
                $_p_info['order_enddate'] = $out_date;
            }

//            mp($_p_info);exit;
            $result = $db->pe_insert('order', $_p_info);
            if ($result) {
                pe_success('登记住户成功', 'admin.php?mod=zhufang');
            } else {
                pe_success('登记失败', 'admin.php?mod=zhufang');
            }

        }


        include(pe_tpl('zf_write.html'));
        break;

    case 'write_ajax':
        $cmp_name = $_p_cmp_name;
        $sqp = "";
        if ($cmp_name != "") {
            $sql = "select * from wxgg_company where company_name like " .
                "";
        }

        $cmp = $db->sql_select($sql);
        if (!empty($cmp)) {
            echo json_encode($cmp);
        } else {
            echo 0;
        }


        break;

    case 'find_fj':
        $house = $db->pe_selectall('house');
        $start_time = strtotime($_g_in_date) ? strtotime($_g_in_date) : "";
        $end_time = strtotime($_g_out_date) ? strtotime($_g_out_date) : "";
        $houseid = $_g_houseid ? $_g_houseid : "";
        $day_start_time = $in_date = strtotime(date('Y-m-d', time()) . ' 12:00:00');  //当天开始时间
        $day_end_time = strtotime(date("Y-m-d") . ' 24:00:00'); //当天结束时间
        $is_find = 1;
        $today_in_number = 0;
        if(!empty($houseid)){
            $sql ='select fj.fj_number,fj.fj_houseid,ho.house_name from wxgg_fj as fj   join wxgg_house as ho on ho.house_id = fj.fj_houseid where fj.fj_houseid = '.$houseid;
        }else{
            $sql ='select fj.fj_number,fj.fj_houseid,ho.house_name from wxgg_fj as fj   join wxgg_house as ho on ho.house_id = fj.fj_houseid ';
        }

        //找房间的订单(方法不对，订单下的时候没有指定房间)
        $find_fj = $db->sql_selectall($sql);
        foreach ($find_fj as $k => $v) {
            if (!empty($houseid)) {

                $sql = "select *  from wxgg_order where  order_housenumber = " . $v['fj_number'] . " and  order_startdate >= " . $start_time . " and order_startdate <= " . $end_time ;
            } else if (empty($start_time) && empty($end_time)) {

                $sql = "select *  from wxgg_order where  order_housenumber = " . $v['fj_number'] . " and  order_startdate >= " . $day_start_time . " and order_startdate <= " . $day_end_time;
            } else {

                $sql = "select *  from wxgg_order where  order_housenumber = " . $v['fj_number'] . " and  order_startdate >= " . $start_time . " and order_startdate <= " . $end_time;
            }

            $order = $db->sql_select($sql);

            if (!empty($order)) {
                $today_in_number = $today_in_number + 1;
//                echo $today_in_number;exit;
                unset($find_fj[$k]);
                $find_fj = array_values($find_fj);
                $find_fj[$k]['fj_state'] = 1;
            }

        }

        //找的到说明是有人住，找不到说明是空闲的

        include(pe_tpl('zhufangmag.html'));
        break;

    case 'check':
        if(isGet()){

            include(pe_tpl('zhufang_check.html'));
        }else if(isPost()){
            $order = $db->pe_select('order',array('order_pw'=>$_p_order_pw));
            if($db->pe_update('order',array('order_id'=>$order['order_id']),array('order_state'=>1,'order_ostate'=>2,'order_usetime'=>date('Y-m-d H-i-m',time()),'order_housenumber'=>$_g_fjnum))){
                pe_success('核销成功');
            }else{
                pe_error('核销失败');
            }
        }

        break;

    default:
        //新客家
//        $_g_name && $sqlwhere .= " and `name` like '%{$_g_name}%'";
//        $_g_qx && $sqlwhere .= " and `qx` = '{$_g_qx}'";
//        $_g_town && $sqlwhere .= " and `town` = '{$_g_town}'";
//        $sqlwhere .= ' order by';
//        $sqlwhere .= " `tp_id` desc";
//        $info_list = $db->pe_selectall('tpmessage', $sqlwhere, '*', array(8, $_g_page));
//        $tongji['all'] = $db->pe_num('tpmessage');
//        $seo = pe_seo($menutitle='景点列表', '', '', 'admin');
//        include(pe_tpl('tp_list.html'));


        $house = $db->pe_selectall('house');
        $t = time();
        $start_time = strtotime(date('Y-m-d', time()) . ' 00:00:00');  //当天开始时间
        $end_time = strtotime(date("Y-m-d") . ' 24:00:00'); //当天结束时间
        //今日入住数
        $today_in_number = 0;

        $fj = $db->sql_selectall('select fj_number,fj_houseid,house_img,house_name from wxgg_fj  join wxgg_house on wxgg_fj.fj_houseid=wxgg_house.house_id  ');
        //剩余房间数
        $today_sy_number = count($fj);

        //识别是不是查找
        $is_find = 0;
        foreach ($fj as $k => $v) {

            $sql = "select *  from wxgg_order where  order_housenumber = " . $v['fj_number'] . " and  order_startdate >= " . $start_time . " and order_startdate <= " . $end_time;
            $order = $db->sql_select($sql);
//            echo $start_time;exit;

            if (!empty($order)) {
                $fj[$k]['fj_state'] = 1;
                $today_in_number = $today_in_number + 1;
                $today_sy_number = $today_sy_number - 1;

                $fj[$k]['order_name'] = $order['order_name'];
                $fj[$k]['order_phone'] = $order['order_phone'];

            } else {
                $fj[$k]['fj_state'] = 0;
            }

        }
//        mp($order);exit;
        $seo = pe_seo($menutitle = '住房管理', '', '', 'admin');
        include(pe_tpl('zhufangmag.html'));
        break;
}
